<?php

//this part of code is to upload compressed images to server's directory and
// only store  image file names in the array

 
/* 
 * Custom function to compress image size and 
 * upload to the server using PHP 
 */ 
function compressImage($source, $destination, $quality) { 
    // Get image info 
    $imgInfo = getimagesize($source); 
    $mime = $imgInfo['mime']; 
     
    // Create a new image from file 
    switch($mime){ 
        case 'image/jpeg': 
            $image = imagecreatefromjpeg($source); 
            break; 
        case 'image/png': 
            $image = imagecreatefrompng($source); 
            break; 
        case 'image/gif': 
            $image = imagecreatefromgif($source); 
            break; 
        default: 
            $image = imagecreatefromjpeg($source); 
    } 
     
    // Save image 
    imagejpeg($image, $destination, $quality); 
     
    // Return compressed image 
    return $destination;
} 
 
 require_once 'createDB.php';
 require_once 'config.php';

// File upload path 
//	This directory if on unix like servers should have 'chmod 777' permissions
//	to be able to upload to. Also it has to be created at the root of the project folder

$targetDir = "./uploads/"; 
 
// If file upload form is submitted 

$status = $statusMsg  = ''; 

if(isset($_POST["Submit"])){ 

    $status = 'error'; 

    if(!empty($_FILES["imageUpload"]["name"])) { 

        // File info 
        $fileName = basename($_FILES["imageUpload"]["name"]); 
        $imageUploadPath = $targetDir. $fileName; 
        
         
        //	This takes extention of file and checks if it's in array
        $fileType = strtolower(pathinfo($imageUploadPath, PATHINFO_EXTENSION)); 
        $allowTypes = array('jpg','png','jpeg','gif'); 
        if(in_array($fileType, $allowTypes)){ 

            // Image temp source 
            $imageTemp = $_FILES["imageUpload"]["tmp_name"]; 

            //If no compression this statement can be used here instead:
            // move_uploaded_file($_FILES["imageUpload"]["tmp_name"],$targetFilePath);
             
            // Compress size and upload image 
            $compressedImagePath = compressImage($imageTemp, $imageUploadPath, 75); 
             
            if($compressedImagePath){ 



				//	insert image file name into database

				// //create the prepared statement
				// $stmt = $conn->prepare("INSERT INTO images(image_name) VALUES(?)");

				// //Bind variables to the prepared statement
				// $stmt->bind_param("s", $param_file_name);

				// //set parameters values	
				// $param_file_name = $fileName;

				// if(!($stmt->execute())){
				// 	echo "We are facing some problem! Try again please...";
				// 	$statusMsg = 'Image Upload Failed! Please Try again.';
				}

			$stmt->close();
		}

                $status = 'success'; 
                $statusMsg = "Image compressed, inserted and uploaded successfully."; 
            }else{ 
                $statusMsg = "Image compress failed!"; 
            } 
        }else{ 
            $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; 
        } 
    }else{ 
        $statusMsg = 'Please select an image file to upload.'; 
    } 
 
// Display status message 
echo $statusMsg; 
echo '<br><a href="gallery.php">View Image Gallery</a>';

?>
